package com.spi.listener;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.spi.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@RocketMQMessageListener(topic = "${rocketmq.consumer.group3.topic}",
        consumerGroup = "${rocketmq.consumer.group3.consumer-group}",
        maxReconsumeTimes = 3)
@Component
@Slf4j
public class AutoBuildConListener implements RocketMQListener<MessageExt> {

    @Autowired
    private AsyncService asyncService ;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] bytes = messageExt.getBody();
            String content = new String(bytes, StandardCharsets.UTF_8);
            Map<String, Object> stringObjectMap = JSON.parseObject(content, new TypeReference<Map<String, Object>>() {});
            String prodKey = (String)stringObjectMap.get("prodKey");
            String sockId = (String)stringObjectMap.get("sockId");
            String contendId = (String)stringObjectMap.get("contendId");
            System.out.println("prodKey==" + prodKey);
            System.out.println("sockId==" + sockId);
            System.out.println("contendId==" + contendId);
            //执行异步任务。。
            asyncService.doAsyncTask(sockId,contendId);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("消费者组survey-topic-autoBuildConclusion消费失败。。。");
            log.info("消费者组survey-topic-autoBuildConclusion消费失败。。。");
        }
    }
}
